﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Net;
using System.IO;
using System.IO.Compression;
using System.Web;
using System.Text.RegularExpressions;

namespace FilmAlert.Common
{
    public class Helpers
    {
        public enum LanguageEnum
        {
            en,
            fr,
            cs,
            da,
            de,
            et,
            es,
            ga,
            it,
            lv,
            lt,
            hu,
            mt,
            nl,
            pl,
            pt,
            ro,
            sk,
            fi,
            sv,
            el,
            bg,
            sl
        }

        #region WebParsing

        public static String GetHtml(String url)
        {
            WebResponse webResponse;
            return GetHtml(url, out webResponse);
        }

        public static String GetHtml(String url, out WebResponse webResponse)
        {
            String html = String.Empty;

            WebRequest webRequest = WebRequest.Create(url);

            //Improve speed and save some bandwidth by accepting compressed response
            webRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");

            try
            {
                webResponse = webRequest.GetResponse();
            }
            catch (Exception ex)
            {
                throw new Exception("An error as occurred while trying to parse the url:" + url, ex);
            }

            if (webResponse.Headers[HttpResponseHeader.ContentEncoding] == "gzip")
                html = new StreamReader(new GZipStream(webResponse.GetResponseStream(), CompressionMode.Decompress)).ReadToEnd();
            else
                html = new StreamReader(webResponse.GetResponseStream()).ReadToEnd();

            return HttpUtility.HtmlDecode(html);
        }
    
        #endregion

        #region Film title functions

        public static String CleanTitle(String filmTitle)
        {
            if (filmTitle.StartsWith("'") && filmTitle.EndsWith("'"))
                filmTitle = filmTitle.Substring(1, filmTitle.Length - 2);

            if (filmTitle.StartsWith("\"") && filmTitle.EndsWith("\""))
                filmTitle = filmTitle.Substring(1, filmTitle.Length - 2);

            return filmTitle;
        }

        public static String CapitalizeAll(String filmTitle)
        {
            return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(filmTitle);
        }

        public static Int16 RemoveYearFromTitle(ref String filmTitle)
        {
            Int16 year = 0;
            const String pattern = @"[(](?<year>[12][089][0-9]{2})[)]";

            MatchCollection matches = Regex.Matches(filmTitle, pattern);
            if (matches.Count > 0)
            {
                year = Convert.ToInt16(matches[matches.Count - 1].Groups["year"].Value);
                filmTitle = filmTitle.Replace("(" + year.ToString() + ")", "").Trim();
            }

            return year;
        }

        #region ConvertFilmTitle

        public static String ConvertFilmTitle(String filmTitle)
        {

            foreach (LanguageEnum language in Enum.GetValues(typeof(LanguageEnum)))
            {
                filmTitle = ConvertFilmTitle(filmTitle, language);
            }

            return filmTitle;
        }

        public static String ConvertFilmTitle(String filmTitle, Helpers.LanguageEnum language)
        {
            if (filmTitle == String.Empty)
                return null;

            String convertedTitle = String.Empty;

            switch (language)
            {
                case LanguageEnum.en:
                    convertedTitle = TitleEnglishConversion(filmTitle);
                    break;

                case LanguageEnum.fr:
                    convertedTitle = TitleFrenchConversion(filmTitle);
                    break;

                case LanguageEnum.de:
                    convertedTitle = TitleGermanConversion(filmTitle);
                    break;

                case LanguageEnum.nl:
                    convertedTitle = TitleDutchConversion(filmTitle);
                    break;

                case LanguageEnum.it:
                    convertedTitle = TitleItalianConversion(filmTitle);
                    break;

                case LanguageEnum.es:
                    convertedTitle = TitleSpanishConversion(filmTitle);
                    break;

                default:
                    convertedTitle = TitleUnkownConversion(filmTitle);
                    break;
            }
            return convertedTitle;
        }

        private static String TitleEnglishConversion(String filmTitle)
        {
            if (filmTitle.EndsWith(", The"))
                filmTitle = "The " + filmTitle.Substring(0, filmTitle.Length - 5);

            if (filmTitle.EndsWith(", An"))
                filmTitle = "An " + filmTitle.Substring(0, filmTitle.Length - 4);

            if (filmTitle.EndsWith(", A"))
                filmTitle = "A " + filmTitle.Substring(0, filmTitle.Length - 3);

            return filmTitle;
        }

        private static String TitleFrenchConversion(String filmTitle)
        {
            if (filmTitle.EndsWith(", Les"))
                filmTitle = "Les " + filmTitle.Substring(0, filmTitle.Length - 5);

            if (filmTitle.EndsWith(", La"))
                filmTitle = "La " + filmTitle.Substring(0, filmTitle.Length - 4);

            if (filmTitle.EndsWith(", Le"))
                filmTitle = "Le " + filmTitle.Substring(0, filmTitle.Length - 4);

            if (filmTitle.EndsWith(", L'"))
                filmTitle = "L' " + filmTitle.Substring(0, filmTitle.Length - 4);

            if (filmTitle.EndsWith(", Des"))
                filmTitle = "Des " + filmTitle.Substring(0, filmTitle.Length - 5);

            if (filmTitle.EndsWith(", De"))
                filmTitle = "De " + filmTitle.Substring(0, filmTitle.Length - 4);

            if (filmTitle.EndsWith(", Une"))
                filmTitle = "Une " + filmTitle.Substring(0, filmTitle.Length - 5);

            if (filmTitle.EndsWith(", Un"))
                filmTitle = "Un " + filmTitle.Substring(0, filmTitle.Length - 4);

            return filmTitle;
        }

        private static String TitleGermanConversion(String filmTitle)
        {
            if (filmTitle.EndsWith(", Der"))
                filmTitle = "Der " + filmTitle.Substring(0, filmTitle.Length - 5);

            if (filmTitle.EndsWith(", Die"))
                filmTitle = "Die " + filmTitle.Substring(0, filmTitle.Length - 5);

            if (filmTitle.EndsWith(", Das"))
                filmTitle = "Das " + filmTitle.Substring(0, filmTitle.Length - 5);

            if (filmTitle.EndsWith(", Eines"))
                filmTitle = "Eines " + filmTitle.Substring(0, filmTitle.Length - 7);

            if (filmTitle.EndsWith(", Einer"))
                filmTitle = "Einer " + filmTitle.Substring(0, filmTitle.Length - 7);

            if (filmTitle.EndsWith(", Eine"))
                filmTitle = "Eine " + filmTitle.Substring(0, filmTitle.Length - 6);

            if (filmTitle.EndsWith(", Ein"))
                filmTitle = "Ein " + filmTitle.Substring(0, filmTitle.Length - 5);

            if (filmTitle.EndsWith(", Am"))
                filmTitle = "Am " + filmTitle.Substring(0, filmTitle.Length - 4);

            if (filmTitle.EndsWith(", Im"))
                filmTitle = "Im " + filmTitle.Substring(0, filmTitle.Length - 4);

            if (filmTitle.EndsWith(", Den"))
                filmTitle = "Den " + filmTitle.Substring(0, filmTitle.Length - 5);

            if (filmTitle.EndsWith(", Dem"))
                filmTitle = "Dem " + filmTitle.Substring(0, filmTitle.Length - 5);

            if (filmTitle.EndsWith(", Dein"))
                filmTitle = "Dein " + filmTitle.Substring(0, filmTitle.Length - 6);

            return filmTitle;
        }

        private static String TitleItalianConversion(String filmTitle)
        {
            //Italian 
            if (filmTitle.EndsWith(", Una"))
                filmTitle = "Una " + filmTitle.Substring(0, filmTitle.Length - 5);

            if (filmTitle.EndsWith(", Un"))
                filmTitle = "Un " + filmTitle.Substring(0, filmTitle.Length - 4);

            if (filmTitle.EndsWith(", I"))
                filmTitle = "I " + filmTitle.Substring(0, filmTitle.Length - 3);

            if (filmTitle.EndsWith(", Il"))
                filmTitle = "Il " + filmTitle.Substring(0, filmTitle.Length - 4);

            if (filmTitle.EndsWith(", L'"))
                filmTitle = "L' " + filmTitle.Substring(0, filmTitle.Length - 4);

            if (filmTitle.EndsWith(", All"))
                filmTitle = "All " + filmTitle.Substring(0, filmTitle.Length - 5);

            if (filmTitle.EndsWith(", La"))
                filmTitle = "La " + filmTitle.Substring(0, filmTitle.Length - 4);

            if (filmTitle.EndsWith(", Le"))
                filmTitle = "Le " + filmTitle.Substring(0, filmTitle.Length - 4);

            if (filmTitle.EndsWith(", Lo"))
                filmTitle = "Lo " + filmTitle.Substring(0, filmTitle.Length - 4);

            if (filmTitle.EndsWith(", Gli"))
                filmTitle = "Gli " + filmTitle.Substring(0, filmTitle.Length - 5);

            if (filmTitle.EndsWith(", Uno"))
                filmTitle = "Uno " + filmTitle.Substring(0, filmTitle.Length - 5);

            if (filmTitle.EndsWith(", Un'"))
                filmTitle = "Un' " + filmTitle.Substring(0, filmTitle.Length - 5);

            return filmTitle;
        }

        private static String TitleSpanishConversion(String filmTitle)
        {
            //Espagnol
            if (filmTitle.EndsWith(", El"))
                filmTitle = "El " + filmTitle.Substring(0, filmTitle.Length - 4);

            if (filmTitle.EndsWith(", Il"))
                filmTitle = "Il " + filmTitle.Substring(0, filmTitle.Length - 4);

            if (filmTitle.EndsWith(", Los"))
                filmTitle = "Los " + filmTitle.Substring(0, filmTitle.Length - 5);

            if (filmTitle.EndsWith(", O"))
                filmTitle = "O " + filmTitle.Substring(0, filmTitle.Length - 3);

            return filmTitle;
        }

        private static String TitleDutchConversion(String filmTitle)
        {
            return filmTitle;
        }

        private static String TitleUnkownConversion(String filmTitle)
        {
            //???
            if (filmTitle.EndsWith(", As"))
                filmTitle = "As " + filmTitle.Substring(0, filmTitle.Length - 4);

            if (filmTitle.EndsWith(", Gli"))
                filmTitle = "Gli " + filmTitle.Substring(0, filmTitle.Length - 5);

            if (filmTitle.EndsWith(", Net"))
                filmTitle = "Net " + filmTitle.Substring(0, filmTitle.Length - 5);

            if (filmTitle.EndsWith(", Al"))
                filmTitle = "Al " + filmTitle.Substring(0, filmTitle.Length - 4);

            if (filmTitle.EndsWith(", Az"))
                filmTitle = "Az " + filmTitle.Substring(0, filmTitle.Length - 4);

            if (filmTitle.EndsWith(", En"))
                filmTitle = "En " + filmTitle.Substring(0, filmTitle.Length - 4);

            if (filmTitle.EndsWith(", Lo"))
                filmTitle = "Lo " + filmTitle.Substring(0, filmTitle.Length - 4);

            if (filmTitle.EndsWith(", Et"))
                filmTitle = "Et " + filmTitle.Substring(0, filmTitle.Length - 4);

            if (filmTitle.EndsWith(", Da"))
                filmTitle = "Da " + filmTitle.Substring(0, filmTitle.Length - 4);

            if (filmTitle.EndsWith(", Een"))
                filmTitle = "Een " + filmTitle.Substring(0, filmTitle.Length - 5);

            if (filmTitle.EndsWith(", Os"))
                filmTitle = "Os " + filmTitle.Substring(0, filmTitle.Length - 4);

            if (filmTitle.EndsWith(", To"))
                filmTitle = "To " + filmTitle.Substring(0, filmTitle.Length - 4);

            if (filmTitle.EndsWith(", Het"))
                filmTitle = "Het " + filmTitle.Substring(0, filmTitle.Length - 5);

            if (filmTitle.EndsWith(", Ta"))
                filmTitle = "Ta " + filmTitle.Substring(0, filmTitle.Length - 4);

            return filmTitle;
        }

        #endregion

        #endregion

    }
}
